package myhttps;

import java.io.IOException;
import java.util.Map;

import org.xutils.common.util.LogUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.callback.Callback;

import android.content.Context;
import android.widget.Toast;
import okhttp3.Call;
import okhttp3.Response;
import okhttp3.ResponseBody;
import utils.ToolLoading;

public class Http {

	private static Http m_Http;

	public static Http getInstant() {
		if (m_Http == null)
			m_Http = new Http();
		return m_Http;
	}

	private Http() {
	}

	public void sendRequest(final Context context,String url, Map<String, String> map, final HttpListener listener) {
		sendRequest(context,url, true, map, listener);
	}

	/**
	 * ֻ����ʾ���ؽ��������
	 * 
	 * @param ctx
	 * @param map
	 */
	public void sendRequest(final Context ctx,String url, Map<String, String> map) {
		sendRequest(ctx,url, true, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				Toast.makeText(ctx, jsonObject.getString("msg"), Toast.LENGTH_LONG).show();
			}

			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub
			}
		});
	}

	public String sendRequestSync(Map<String, String> map) {
		PostFormBuilder builder = OkHttpUtils.post().url(HttpConst.BASEURL);
		if (map != null)
			for (Map.Entry<String, String> entry : map.entrySet()) {
				builder.addParams(entry.getKey(), entry.getValue());
			}
		try {
			Response response = builder.build().execute();
			ResponseBody body = response.body();
			return body.string();
		} catch (IOException e) {
		}
		return null;
	}
	
	public String sendRequestSync(String url,Map<String, String> map) {
		PostFormBuilder builder = OkHttpUtils.post().url(url);
		if (map != null)
			for (Map.Entry<String, String> entry : map.entrySet()) {
				builder.addParams(entry.getKey(), entry.getValue());
			}
		try {
			Response response = builder.build().execute();
			ResponseBody body = response.body();
			return body.string();
		} catch (IOException e) {
		}
		return null;
	}

	public void test(final Context context, final boolean isshowDialog, Map<String, String> map,
			final HttpListener listener) {
		if (isshowDialog)
			ToolLoading.ShowLoading(context);
		PostFormBuilder builder = OkHttpUtils.post().url(HttpConst.BASEURL);
		if (map != null)
			for (Map.Entry<String, String> entry : map.entrySet()) {
				builder.addParams(entry.getKey(), entry.getValue());
			}
		builder.build().execute(new Callback<String>() {

			@Override
			public String parseNetworkResponse(Response response) throws Exception {
				String result = response.body().string();
				return result;
			}

			@Override
			public void onError(Call request, Exception e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onResponse(String result) {
				// TODO Auto-generated method stub
				listener.onHttpSuccess(result);
			}
		});
	}

	public void sendRequest(final Context context, final boolean isshowDialog, final String url,
			Map<String, String> map, final HttpListener listener) {
		if (isshowDialog)
			ToolLoading.ShowLoading(context);
		PostFormBuilder builder = OkHttpUtils.post().url(url);
		if (map != null)
			for (Map.Entry<String, String> entry : map.entrySet()) {
				builder.addParams(entry.getKey(), entry.getValue());
			}
		builder.build().execute(new com.zhy.http.okhttp.callback.Callback<String>() {

			@Override
			public String parseNetworkResponse(Response response) throws Exception {
				// TODO Auto-generated method stub
				String result = response.body().string();
				return result;
			}

			@Override
			public void onError(Call request, Exception e) {
				if (isshowDialog)
					ToolLoading.closeLoading(context);
				Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
				listener.onHttpError(e.toString());
			}

			@Override
			public void onResponse(String result) {
				String error = "";
				if (isshowDialog)
					ToolLoading.closeLoading(context);
				if (error.equals("")) {
					LogUtil.e(result);
					listener.onHttpSuccess(result);
				} else {
					Toast.makeText(context, error, Toast.LENGTH_SHORT).show();
					listener.onHttpError(error);
				}
			}
		});
	}

	public void sendRequest(final Context context,String url, final boolean isshowDialog, Map<String, String> map,
			final HttpListener listener) {
		sendRequest(context, isshowDialog, url, map, listener);

	}

	private String checkResult(String result) {
		if (result == null || result.equals("null") || result.equals(""))
			return "���������ؿգ���Ҳ��֪���ǳɹ��˻���ʧ����.............";
		try {
			JSONObject object = JSON.parseObject(result);
			return object.getBoolean("done") == true ? "" : object.getString("msg");
		} catch (Exception e) {
			return "";
		}
	}

}
